/**
 * 
 */
package com.gmail.yesguxun.flee.phonegap.plugin;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
import com.stackmob.android.sdk.common.StackMobCommon;
import com.stackmob.sdk.api.StackMob;
import com.stackmob.sdk.api.StackMobQuery;
import com.stackmob.sdk.callback.StackMobCallback;
import com.stackmob.sdk.exception.StackMobException;


//http://wiki.phonegap.com/w/page/36753494/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20Android#WriteTestCaseforthePlugin

/**
 * @author noguxun
 *
 */
public class FleeInfoPlugin extends Plugin {
    
    class StackMobHandler implements StackMobCallback{
        
        String mResult = null;
        FleeInfoPlugin mFlee = null;
        
        public StackMobHandler(FleeInfoPlugin fleeInfo)
        {
            mFlee = fleeInfo;
            mResult = null;
        }
        
        @Override
        public void success(String responseBody) {
            mResult = responseBody;
            Log.i("StackMob", mResult);
            mFlee.mStackMobCallSignal.countDown();
        }
        
        @Override
        public void failure(StackMobException e) {
            mResult = null;
            Log.i("StackMob", "failure");
            mFlee.mStackMobCallSignal.countDown();
        }
        
        public String getResult()
        {
            return mResult;
        }
    }
    
    private StackMob mStackMob;
    CountDownLatch mStackMobCallSignal ;

    
    public FleeInfoPlugin()
    {
        //key information https://www.stackmob.com/platform/account/apps/reset
        //about set up http://support.stackmob.com/requests
        StackMobCommon.API_KEY = "b4958d0c-d955-4395-97e3-b48480ffc6c4";
        StackMobCommon.API_SECRET = "db4a3248-d849-4cc2-b2f7-2eec65b6ac70";
        StackMobCommon.USER_OBJECT_NAME = "";
        StackMobCommon.API_VERSION = 0;              
        //StackMobCommon.API_URL_FORMAT = "jinbo14.mob1.stackmob.com/api";
        
        mStackMob = StackMobCommon.getStackMobInstance();   
    }

    /* (non-Javadoc)
     * @see com.phonegap.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
     */
    @Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
        JSONArray fleeArray = null;
        PluginResult pluginResult = null;
        
        String fleeInfoString = getFleeInfo();
        if(fleeInfoString != null)
        {
            try {
                fleeArray = new JSONArray(fleeInfoString);
                pluginResult = new PluginResult(Status.OK, fleeArray);
            } catch (JSONException e) {
                pluginResult = new PluginResult(Status.JSON_EXCEPTION, fleeArray);
            }
        }
        else
        {
            pluginResult = new PluginResult(Status.ERROR);
        }
        
        return pluginResult;
    }
    
    
    //https://www.stackmob.com/platform/stackmob/help/topics/Getting-Started:-Java-Client-SDK#a-advanced_queries
    private String getFleeInfo()
    {
        StackMobQuery q = new StackMobQuery("flee").field("time_start").isGreaterThan("197212201220").getQuery();
        StackMobHandler stackMobHandler = new StackMobHandler(this);
        
        mStackMobCallSignal = new CountDownLatch(1);
          
        mStackMob.get(q, stackMobHandler);
        
        try {
            mStackMobCallSignal.await(3000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return stackMobHandler.getResult();
    }

}
